package pers.qianyu.month_202012.date_20201222;

import java.util.*;

/**
 * 355. 设计推特
 * https://leetcode-cn.com/problems/design-twitter/
 * - 练习合并 N 个有序链表
 *
 * @author mizzle rain
 * @date 2020年12月22日22:20:44
 */
public class Twitter {
    /**
     * Initialize your data structure here.
     */
    public Twitter() {
    }

    /**
     * Compose a new tweet.
     */
    public void postTweet(int userId, int tweetId) {
        if (!userMap.containsKey(userId)) {
            userMap.put(userId, new User(userId));
        }
        userMap.get(userId).post(tweetId);
    }

    /**
     * Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent.
     */
    public List<Integer> getNewsFeed(int userId) {
        if (!userMap.containsKey(userId)) {
            return new ArrayList<>();
        }
        User user = userMap.get(userId);
        HashSet<Integer> friendIds = user.friends;
        PriorityQueue<Tweet> tweets = new PriorityQueue<>(user.friends.size(),
                (o1, o2) -> o2.time - o1.time);
        for (Integer friendId : friendIds) {
            Tweet head = userMap.get(friendId).head;
            if (head != null) {
                tweets.add(head);
            }
        }
        ArrayList<Integer> res = new ArrayList<>();
        while (!tweets.isEmpty()) {
            if (res.size() == 10) {
                return res;
            }
            Tweet top = tweets.poll();
            res.add(top.id);
            if (top.next != null) {
                tweets.add(top.next);
            }
        }
        return res;
    }

    /**
     * Follower follows a followee. If the operation is invalid, it should be a no-op.
     */
    public void follow(int followerId, int followeeId) {
        if (!userMap.containsKey(followerId)) {
            userMap.put(followerId, new User(followerId));
        }
        if (!userMap.containsKey(followeeId)) {
            userMap.put(followeeId, new User(followeeId));
        }
        userMap.get(followerId).follow(followeeId);
    }

    /**
     * Follower unfollows a followee. If the operation is invalid, it should be a no-op.
     */
    public void unfollow(int followerId, int followeeId) {
        if (userMap.containsKey(followerId)) {
            User user = userMap.get(followerId);
            user.unFollow(followeeId);
        }
    }

    private static int timestamp = 0;
    private HashMap<Integer, User> userMap = new HashMap<>();

    private static class User {
        int id;
        HashSet<Integer> friends = new HashSet<>();
        Tweet head;

        public User(int id) {
            this.id = id;
            friends.add(id);
        }

        public void follow(int id) {
            friends.add(id);
        }

        public void unFollow(int id) {
            // 不能取关自己
            if (this.id == id) {
                return;
            }
            friends.remove(id);
        }

        public void post(int msg) {
            // 头插法
            head = new Tweet(msg, timestamp++, head);
        }
    }

    private static class Tweet {
        int id;
        int time;
        Tweet next;

        public Tweet(int id, int time, Tweet next) {
            this.id = id;
            this.time = time;
            this.next = next;
        }
    }

    public static void main(String[] args) {
        Twitter twitter = new Twitter();
        twitter.postTweet(1, 1);
        System.out.println(twitter.getNewsFeed(1));
        twitter.follow(2, 1);
        System.out.println(twitter.getNewsFeed(2));
        twitter.unfollow(2, 1);
        System.out.println(twitter.getNewsFeed(2));
    }
}

/**
 * Your Twitter object will be instantiated and called as such:
 * Twitter obj = new Twitter();
 * obj.postTweet(userId,tweetId);
 * List<Integer> param_2 = obj.getNewsFeed(userId);
 * obj.follow(followerId,followeeId);
 * obj.unfollow(followerId,followeeId);
 */